package com.bz.cart.service.impl;

import com.bz.cart.feign.CommonItemFeignClient;
import com.bz.cart.service.CookieCartService;
import com.bz.dto.CartDto;
import com.bz.pojo.TbItem;
import com.bz.utils.CookieUtils;
import com.bz.utils.JsonUtils;
import com.bz.utils.Result;
import com.bz.vo.CartItemVo;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@Slf4j
@Service
public class CookieCartServiceImpl implements CookieCartService {

    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private CommonItemFeignClient commonItemFeignClient;

    private String cart_cookie_name = "cart:cookie:name:";

    @Override
    public Result addItem(Long itemId, Integer num, HttpServletRequest request, HttpServletResponse response) {
        //1,获取临时购物车
        CartDto cartDto = this.getCart(request);
        //2,查询商品
        TbItem tbItem = this.selectItemById(itemId);
        //3,向购物车中添加商品
        this.addItemToCart(cartDto,tbItem,num,itemId);
        //4,将购物车通过Cookie写回给客户端浏览器
        this.addClientCookie(request,response,cartDto);
        return Result.ok();
    }

    /**
     * 4,将购物车通过Cookie写回给客户端浏览器
     */
    private void addClientCookie(HttpServletRequest request,HttpServletResponse response,CartDto cartDto){
        Map<String, CartItemVo> cartItemVoMap = cartDto.getCartItemVoMap();
        String objectToJson = JsonUtils.objectToJson(cartItemVoMap);
        CookieUtils.setCookie(request,response,this.cart_cookie_name,objectToJson,true);
    }

    /**
     * 3.将商品添加到购物车中
     * @param cartDto
     * @param item
     * @param num
     * @param itemId
     */
    private void addItemToCart(CartDto cartDto,TbItem item,Integer num,Long itemId){
        // 从购物车中取商品
        Map<String, CartItemVo> cartItemVoMap = cartDto.getCartItemVoMap();
        CartItemVo cartItemVo = cartItemVoMap.get(itemId.toString());
        if(cartItemVo == null){
            //没有相同的商品
            CartItemVo cartItem = new CartItemVo();
            cartItem.setId(item.getId());
            cartItem.setImage(item.getImage());
            cartItem.setNum(num);
            cartItem.setPrice(item.getPrice());
            cartItem.setSellPoint(item.getSellPoint());
            cartItem.setTitle(item.getTitle());
            cartItemVoMap.put(item.getId().toString(),cartItem);
        }else{
            cartItemVo.setNum(cartItemVo.getNum()+num);
        }
    }

    /**
     * 2,根据商品ID查询商品
     * @return
     */
    private TbItem selectItemById(Long itemId){
        return commonItemFeignClient.selectItemInfo(itemId);
    }

    /**
     * 1.获取购物车数据
     * @param request
     * @return
     */
    private CartDto getCart(HttpServletRequest request){
        //临时购物车已存在
        String cartJson = CookieUtils.getCookieValue(request, this.cart_cookie_name, true);
        if (cartJson == null){
            //临时购物车不存在
            return new CartDto();
        }
        //如果含有临时购物车，那么需要做json转换
        try {
            return objectMapper.readValue(cartJson, CartDto.class);
        } catch (IOException e) {
            log.error("【解析购物车数据失败】,{}",e);
        }
        return new CartDto();
    }
}
